// pages/app/user/index/index.js
const app = getApp();
var host = app.globalData.host;
var utils = app.globalData.utils;
var self = null;

Page({
  data: {
    themeColor: '',
    themeColor2: '',
    themeColor3: '',
    defaultColor: '',
    borderColor: '',
    info: null
  },
  onLoad: function(options) {
    self = this;
    self.getStyle();
  },
  onShow: function() {
    wx.checkSession({
      success() {
        app.userInfoReadyCallback = function () {
          self.init();
        }
        app.login(self.route, true);
      },
      fail() {
        // app.loginWechat(app.login(self.route));
        app.globalData.openId = null;
        app.serverLogin({
          success() {
            console.log('自动登录成功')
          },
          fail() {
            console.log('自动登录失败')
          }
        })
      }
    })
  },
  getStyle() {
    self.setData({
      themeColor: app.globalData.themeColor,
      themeColor2: app.globalData.themeColor2,
      themeColor3: app.globalData.themeColor3,
      defaultColor: app.globalData.defaultColor,
      borderColor: app.globalData.borderColor
    })
  },
  init() {
    // var url = host + '/lx/mini/user/info';
    // var params = {};
    // var success = function (res) {
    //   self.setData({
    //     info: res.info
    //   })
    // }
    // var fail = function () { };
    // var complete = function () { };
    // utils.request(url, params, success, fail, complete);
    if (app.globalData.userInfo) {
      self.setData({
        info: app.globalData.userInfo
      })
    } else {
      app.userInfoReadyCallback = res => {
        self.setData({
          info: res.info
        })
        app.userInfoReadyCallback = null;
      }
    }
  },
  goUserInfo(event) {
    wx.navigateTo({
      // url: '/pages/app/user/userInfo/userInfo'
      url: '/pages/app/user/signUp/signUp?editMode=1'
    })
  },
  goPoint(event) {
    wx.navigateTo({
      url: '/pages/app/user/point/point'
    })
  },
  goMyCerts(event) {
    if (self.data.info.status == 3) {
      wx.showModal({
        title: '您没有通过审核',
        content: '不能查看证书。' + '原因：' + self.data.info.content,
        showCancel: false
      })
      return;
    }
    wx.navigateTo({
      url: '/pages/app/user/myCerts/myCerts'
    })
  },
  logout(event) {
    wx.showModal({
      title: '退出登录',
      content: '确认退出当前账号？',
      success: function(res) {
        if (res.confirm) {
          wx.switchTab({
            url: '/pages/app/schools/index/index'
          })
          // wx.removeStorageSync("openId");
          wx.removeStorageSync("user");
          app.globalData.userInfo = null;
          wx.navigateTo({
            url: '/pages/app/user/login/login'
          })
        }
      }
    })
  }
})